import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**战报-释放技能属性枚举*/
export const enum E_BriReleaseSkill {
	/**释放角色id*/
	ACTOR_ID = 0,
	/**技能id*/
	SKILL_ID = 1,
	/**技能配置id*/
	SKILL_CONFIG_ID = 2,
	/**连击*/
	DRIBBLE = 3,
	/**枚举数量*/
	EnumCount = 4
}


/**战报-释放技能*/
export class BriReleaseSkill {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BRI_RELEASE_SKILL_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BRI_RELEASE_SKILL_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BriReleaseSkill[] = [];

	/**创建入口*/
	public static create(json?: string): BriReleaseSkill {
		const c = this.POOL.pop() || new BriReleaseSkill();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**释放角色id (uint16)*/
	protected _actorId: number;
	/**技能id (uint32)*/
	protected _skillId: number;
	/**技能配置id (uint32)*/
	protected _skillConfigId: number;
	/**连击*/
	protected _dribble: boolean;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BriReleaseSkill.POOL.indexOf(this) === -1) {
			BriReleaseSkill.POOL.push(this);
		}
	}

	/**释放角色id*/
	public get actorId() {
		return this._actorId;
	}


	/**释放角色id*/
	public set actorId(v: number) {
		this._actorId = v;
	}


	/**技能id*/
	public get skillId() {
		return this._skillId;
	}


	/**技能id*/
	public set skillId(v: number) {
		this._skillId = v;
	}


	/**技能配置id*/
	public get skillConfigId() {
		return this._skillConfigId;
	}


	/**技能配置id*/
	public set skillConfigId(v: number) {
		this._skillConfigId = v;
	}


	/**连击*/
	public get dribble() {
		return this._dribble;
	}


	/**连击*/
	public set dribble(v: boolean) {
		this._dribble = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BriReleaseSkill): any {
		switch (k) {
			case E_BriReleaseSkill.ACTOR_ID: return this._actorId;
			case E_BriReleaseSkill.SKILL_ID: return this._skillId;
			case E_BriReleaseSkill.SKILL_CONFIG_ID: return this._skillConfigId;
			case E_BriReleaseSkill.DRIBBLE: return this._dribble;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BriReleaseSkill, v: any): void {
		switch (k) {
			case E_BriReleaseSkill.ACTOR_ID: this._actorId = v; break;
			case E_BriReleaseSkill.SKILL_ID: this._skillId = v; break;
			case E_BriReleaseSkill.SKILL_CONFIG_ID: this._skillConfigId = v; break;
			case E_BriReleaseSkill.DRIBBLE: this._dribble = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//释放角色id
		this._actorId = 0;
		//技能id
		this._skillId = 0;
		//技能配置id
		this._skillConfigId = 0;
		//连击
		this._dribble = false;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._actorId) {
			json.actorId = this._actorId;
		}
		if (this._skillId) {
			json.skillId = this._skillId;
		}
		if (this._skillConfigId) {
			json.skillConfigId = this._skillConfigId;
		}
		if (this._dribble) {
			json.dribble = this._dribble;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.actorId) {
			this._actorId = json.actorId;
		}
		if (json.skillId) {
			this._skillId = json.skillId;
		}
		if (json.skillConfigId) {
			this._skillConfigId = json.skillConfigId;
		}
		if (json.dribble) {
			this._dribble = json.dribble;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint16(this._actorId);
		byte.writeUint32(this._skillId);
		byte.writeUint32(this._skillConfigId);
		byte.writeBool(this._dribble);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._actorId = byte.readUint16();
		this._skillId = byte.readUint32();
		this._skillConfigId = byte.readUint32();
		this._dribble = byte.readBool();
		return this;
	}


	/**克隆*/
	public clone(): BriReleaseSkill {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BriReleaseSkill.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BriReleaseSkill.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//释放角色id
			if (idx === E_BriReleaseSkill.ACTOR_ID) {
				const oldv = this._actorId;
				this._actorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._actorId, oldv);
				i += 2;
				continue;
			}
			//技能id
			if (idx === E_BriReleaseSkill.SKILL_ID) {
				const oldv = this._skillId;
				this._skillId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._skillId, oldv);
				i += 2;
				continue;
			}
			//技能配置id
			if (idx === E_BriReleaseSkill.SKILL_CONFIG_ID) {
				const oldv = this._skillConfigId;
				this._skillConfigId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._skillConfigId, oldv);
				i += 2;
				continue;
			}
			//连击
			if (idx === E_BriReleaseSkill.DRIBBLE) {
				const oldv = this._dribble;
				this._dribble = us[i + 1];
				IAsync.dispatch(eventID, idx, this._dribble, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BriReleaseSkill with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BriReleaseSkill): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._actorId !== other._actorId) {
			updates.push(E_BriReleaseSkill.ACTOR_ID, this._actorId);
		}
		if (this._skillId !== other._skillId) {
			updates.push(E_BriReleaseSkill.SKILL_ID, this._skillId);
		}
		if (this._skillConfigId !== other._skillConfigId) {
			updates.push(E_BriReleaseSkill.SKILL_CONFIG_ID, this._skillConfigId);
		}
		if (this._dribble !== other._dribble) {
			updates.push(E_BriReleaseSkill.DRIBBLE, this._dribble);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BriReleaseSkill.TOTAL_EVENT_ID, this.getV(0));
	}

}
